var dataUrl = webConfig.webUrl + '/sseUser/getUserAllInfoList',
    grid_selector = "#grid-table",
    pager_selector = "#grid-pager";

//查询
function query() {
    var realName = $.trim(document.getElementById('realName').value),
        userName = $.trim(document.getElementById('userName').value);

    var rnStr = /^[a-zA-Z\u4e00-\u9fa5]+$/;
    if (realName && !rnStr.test(realName)) {
        parent.layer.alert('请输入正确的姓名', {
            icon: 2
        });
        return;
    }
    var lnStr = /^[a-zA-Z0-9\u4e00-\u9fa5]+$/;
    if (userName && !lnStr.test(userName)) {
        parent.layer.alert('请输入正确的登录名', {
            icon: 2
        });
        return;
    }


    var reloadParams = {
        url: dataUrl,
        postData: setPostData()
    };

    $(grid_selector).jqGrid("setGridParam", reloadParams).trigger("reloadGrid", [{
        page: 1
    }]);
}

$(function() {
    $(grid_selector).jqGrid({
        height: 'auto',

        //是否允许显示/隐藏，只有caption不为空时有效
        hidegrid: false,

        //table → caption
        caption: "预警人员列表",

        //是否允许全选
        multiselect: true,

        //全选的宽度(只有multiselect:true时)
        multiselectWidth: 30,

        //显示的标题
        colNames: ['姓名', '登录名', '手机号码', '所属角色', '用户状态', ''],

        //列属性
        colModel: [{
            name: 'realName',
            resizable: false,
            align: 'center'
        }, {
            name: 'userName',
            resizable: false,
            align: 'center'
        }, {
            name: 'phone',
            resizable: false,
            align: 'center'
        }, {
            name: 'roleName',
            resizable: false,
            align: 'center'
        }, {
            name: 'status',
            resizable: false,
            align: 'center',
            formatter: function(x, y, z) {
                if (x == 1) {
                    return '可用'
                }
                if (x == 2) {
                    return '禁用'
                }
            }
        }, {
            name: 'id',
            hidden: true
        }],

        //可选显示的行数
        rowList: webConfig.pager.ROWLIST,

        //默认显示行数
        rowNum: webConfig.pager.PAGESIZE,

        //总条数
        viewrecords: true,

        //分页层 id
        pager: pager_selector,

        //分页参数
        prmNames: {
            page: 'pageIndex',
            rows: "pageSize",
            order: null,
            search: null,
            sort: null,
            nd: null,
            sidx: null
        },

        //提交方式，默认get
        mtype: 'POST',

        postData: setPostData(),

        //数据格式
        datatype: "json",

        jsonReader: {
            repeatitems: true, //是否允许乱序显示数据,true:不可以
            root: "result.list", //json中代表实际模型数据的入口
            total: "result.pages", //总页数
            page: "result.pageNum", //当前页
            records: "result.total" //总条数
        },

        //数据来源地址
        url: dataUrl,

        loadComplete: function(xhr) {
            checkResultCode({
                code: xhr.code,
                msg: xhr.message
            }, xhr.message);
            var table = this;
            setTimeout(function() {
                updatePagerIcons(table);
            }, 0);
        },
        onSelectRow: function(rowId) {
            var c = getQueryString('c') || 0,
                rowIds = $(grid_selector).jqGrid('getGridParam', 'selarrrow'),
                canLen = 3-c;
            if (rowIds.length > canLen) {
                $(grid_selector).setSelection(rowId);
                parent.layer.alert('最多选择' + canLen + '个预警人员', {
                    icon: 2
                });
                return false;
            }
            return true;
        }

    }).jqGrid('setGridWidth', $(".page-content-area").width());

    //设置grid宽度为自适应,适应比例根据每列设置的宽度
    $(window).on('resize.jqGrid', function() {
        setIframeHeight();
        $(grid_selector).jqGrid('setGridWidth', $(".page-content-area").width());
    });

});

//设置参数
function setPostData() {
    return {
        realName: $.trim(document.getElementById('realName').value),
        userName: $.trim(document.getElementById('userName').value),
        roleNameId: document.getElementById('role').value,
        status: document.getElementById('userStatus').value,

        pageSize: webConfig.pager.PAGESIZE
    };
}


//所属角色下拉框
$.ajax({
    url: webConfig.webUrl + '/role/getSysRoleList',
    type: 'POST',
    dataType: 'json',
    success: function(data) {
        checkResultCode({
            code: data.code,
            msg: data.message
        }, data.message, 0, 0, function() {
            setSelectOptions('role', data.result, -1, {
                text: 'roleName',
                value: 'id'
            });
        });

    }
})